package company.permissionsystem.dao;

import company.permissionsystem.model.entity.Department;
import company.permissionsystem.model.entity.SysUser;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface DepartmentDao {

    @Select("select * from sys_department where id=#{id}")
    Department findDepartmentById(Integer id);

    @Select("select * from sys_department")
    List<Department> findAllDepartment();

    @Select("select * from sys_user where department_id=#{id}")
    @Results(id = "Map", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "department_id", property = "departmentId"),
            @Result(column = "position_id", property = "positionId"),

            @Result(property = "department", column = "department_id",
                    one = @One(select = "company.permissionsystem.dao.DepartmentDao.findDepartmentById", fetchType = FetchType.LAZY)),
            @Result(property = "position", column = "position_id",
                    one = @One(select = "company.permissionsystem.dao.PositionDao.findPositionById", fetchType = FetchType.LAZY)),
    })
    List<SysUser> findEmployeeByDepartmentId(Long id);
}
